package us.gaaoc.framework.webapp.util;

import java.util.HashMap;
import java.util.Map;

import us.gaaoc.framework.dao.ModelFacade;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.model.Person;
import us.gaaoc.framework.service.TagService;

/**
 * This is a dummy map interface that allows JSF 1.1 to provide a parameter to
 * the 'isTagged' function. It also caches the results in a map to provide
 * faster access within a single JSF request.
 * 
 * This class can be removed when upgrading to JSF 1.2.7 or above, Enabling
 * JBoss EL, and replacing references to checkTag with a proper function call to
 * 'isTagged'.
 * 
 * @author artripa
 * 
 */
public class CheckTag extends DummyMap implements Map {

	TagService tagService;
	Person p;
	ModelFacade mf;
	Map<String, Boolean> cachedResults = new HashMap<String, Boolean>();

	public CheckTag(TagService tagService, Person p, ModelFacade mf) {
		this.p = p;
		this.mf = mf;
		this.tagService = tagService;
	}

	public boolean checkTag(CaseRecord cr) {
		return tagService.isTagged(cr, p);
	}

	public Object get(Object obj) {

		if (cachedResults.containsKey(((String) obj))) {
			return cachedResults.get(((String) obj));
		} else {
			CaseRecord caseRecord = (CaseRecord) mf.getSession().get(
					CaseRecord.class, ((String) obj));
			boolean ret;
			cachedResults.put(((String) obj), ret = checkTag(caseRecord));
			return ret;
		}
	}

	public void invalidateCacheEntry(String uuid) {
		this.cachedResults.remove(uuid);
	}

}
